<?php


namespace app\api\controller;


use app\admin\model\Admin;
use app\common\model\ProductBuy;
use app\common\controller\Api;
use think\Db;

class Product extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $model = null;
    protected $user = null;

    public function _initialize()
    {
        parent::_initialize();

        $this->model = new \app\common\model\Product;
        $this->user = $this->auth ? $this->auth->getUser() : null;
    }
	
	/**
	 * 收益结算
	 */
	public function balance()
	{
        \app\common\model\ProductBuy::buyBalance();
	}
	
    /**
     * 产品列表
     */
    public function list()
    {
        $list = $this->model::getList();
        $result = array("total" => $list->total(), "rows" => $list->items());
        $this->success(__('返回成功'), $result);
    }

    /**
     * 产品详情
     */
    public function detail($id = null)
    {
        if(!$id){
            $this->error(__('产品信息错误'));
        }
        $where = ['id' => $id];

        $info = $this->model::get($where);

        $this->success(__('返回成功'), $info);
    }

    /**
     * 产品预览
     */
    public function preview()
    {
        $product_id = $this->request->get('product_id'); //产品ID

        if(!$product_id){
            $this->error(__('产品信息错误'));
        }

        $where = ['id' => $product_id, 'status' => 2];
        $productInfo = $this->model::getInfo($where);

        if(!$productInfo){
            $this->error(__('产品已关闭/已结束/已投满'));
        }

        $this->success(__('返回成功'), [
            'productInfo' => $productInfo,
            'userInfo' => $this->auth->getUserinfo(),//用户信息
        ]);
    }

    /**
     * 产品购买
     */
    public function buy()
    {
        $product_id = $this->request->post('product_id'); //产品ID
        $price = $this->request->post('price'); //投资金额
        $msg = $this->request->post('msg'); //金额说明

        if(!$product_id){
            $this->error(__('产品信息错误'));
        }

        $where = ['id' => $product_id, 'status' => 1];
        $productInfo = $this->model::get($where);

        if(!$productInfo){
            $this->error(__('产品已关闭/已结束/已投满'));
        }

        $res = ProductBuy::get(['product_id' => $productInfo->id, 'status' => 1]);

        if($res && $productInfo->is_back == 0) {
            $this->error(__('当前产品不可重复购买'));
        }

        // 启动事务
        Db::startTrans();
        try{

            $model = new ProductBuy();

            //用户购买产品
            $productData = [
                'product_id' => $product_id,
                'user_id' => $this->user->id,
                'admin_id' => Admin::getAdminId($this->user->p_id),
                'username' => $this->user->username,
                'price' => $price,
                'buytime' => time(),
                'nexttime' => (time() + 24*60*60),
                'way' => $productInfo->way,
                'ratio' => $productInfo->ratio,
            ];

            $model::create($productData);

            //资金明细记录日志
//            \app\common\model\User::money(-$price, $this->user->id, $msg);
            // 提交事务
            Db::commit();

        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success(__('创建成功'));
    }
}